package com.redteam.xomtro.adapters;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.redteam.xomtro.R;

import java.util.ArrayList;

public class SubImageAdapter extends FragmentPagerAdapter implements
        IconPagerAdapter {

    private final ArrayList<Integer> imagesArr;
    private final ImageLoader imageLoader;
    private int mCount = 0;
    private DisplayImageOptions options;

    public SubImageAdapter(FragmentManager fm, ArrayList<Integer> images, ImageLoader imageLoader) {
        super(fm);
        this.imagesArr = images;
        mCount = images.size();

        this.imageLoader = imageLoader;
        this.options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.im_default_small)
                .showImageOnFail(R.drawable.im_default_small)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                .build();
    }

    @Override
    public Fragment getItem(int position) {
        //// demo data
        return new ScreenSlidePageFragment(imagesArr.get(position));
        ////

//        return new ScreenSlidePageFragment(imagesArr.get(position));
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public int getIconResId(int index) {
        return 0;
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }

    public class ScreenSlidePageFragment extends Fragment {

        private String imageUrl;
        int imgId;

        //// demo data
        public ScreenSlidePageFragment(int url) {
            this.imgId = url;
        }
        ////

//        public ScreenSlidePageFragment(String url) {
//            this.imageUrl = url;
//        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            ImageView imagesView = new ImageView(getActivity());
            imagesView.setImageResource(imgId);
            imagesView.setScaleType(ImageView.ScaleType.CENTER_CROP);


//            imageLoader.displayImage(imageUrl, imagesView, options);
            return imagesView;
        }
    }


}

interface IconPagerAdapter {
    /**
     * Get icon representing the page at {@code index} in the adapter.
     */
    int getIconResId(int index);

    // From PagerAdapter
    int getCount();
}
